
#include <BReadWrite>
#include <BOpenGL>
#include <BPainter>
#include <BTexture>
#include <BImage>
#include <BMouseEvent>
#include "DragWidget.h"

DragWidget::DragWidget(BWidget* parent) : BWidget(parent)
{
	//this->setLayered(true);
	this->setBorder(2);
	this->setPerchs(5);
	_color.set(128, 200, 210, 255);
	_pressed = false;
	_image0 = Read<BImage>("data/image/Multimedia_Icons_005.png");
	_image1 = Read<BImage>("data/image/gfd.jpg");
}
DragWidget::~DragWidget()
{

}

void DragWidget::proc(BObject* object, const BString& signal, const BValue& value)
{
	int a = 0;
}
void DragWidget::procMoving(BObject* object, const BValue& value)
{
	int a = 0;
}

void DragWidget::paintEvent(const BEvent& event)
{
	int width = this->width();
	int height = this->height();

	BPainter painter(this);

	if (_pressed)
		painter.drawImage(0, 0, width, height, _image0);
	else
		painter.drawImage(0, 0, width, height, _image1);

	painter.drawBorder();
	painter.drawPerch();
	painter.drawMargin();
}

void DragWidget::mousePressEvent(const BMouseEvent& mouseEvent)
{
	if(mouseEvent.button() == Button_Left)
	{
		_pressed = true;
		_pressedPos = mouseEvent.pos();
		this->fresh();
	}
}
void DragWidget::mouseReleaseEvent(const BMouseEvent& mouseEvent)
{
	if(mouseEvent.button() == Button_Left)
	{
		_pressed = false;
		this->fresh();
	}
}
void DragWidget::mouseMoveEvent(const BMouseEvent& mouseEvent)
{
	BPoint pos = mouseEvent.pos();
	if(_pressed)
	{
		BPoint def = pos - _pressedPos;
		this->move(pos - _pressedPos);
		emit("moving", def);
		this->fresh();
	}
}
